package club.xiaojiawei.binarytree;

import java.util.*;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 5/9/22 9:18 PM
 * @question 102. 二叉树的层序遍历
 * @description 给你二叉树的根节点 root ，返回其节点值的 层序遍历 。 （即逐层地，从左到右访问所有节点）。
 */
public class LevelOrder102 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        left.left = new TreeNode(4);
        root.left = left;
        TreeNode right = new TreeNode(3);
        right.right = new TreeNode(5);
        root.right = right;
        List<List<Integer>> result = levelOrder(root);
        result.forEach(System.out::println);
    }

    /**
     * 迭代，队列（不推荐用两个指针）
     * 思路：图的广度优先搜索
     * @param root
     * @return
     */
    public static List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> result = new ArrayList<>();
        if (root == null){
            return result;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(root.val);
        result.add(list);
        queue.add(root);
//        用两个指针保证同层的节点在同一个List里
        int now = 0, pre = 1;
        while (queue.size() > 0){
            if (now == 0){
                list = new ArrayList<>();
            }
            TreeNode current = queue.pop();
            pre--;
            if (current.left != null){
                now++;
                list.add(current.left.val);
                queue.add(current.left);
            }
            if (current.right != null){
                now++;
                list.add(current.right.val);
                queue.add(current.right);
            }
            if (pre == 0 && list.size() > 0){
                result.add(list);
                pre = now;
                now = 0;
            }
        }
        return result;
    }

    /**
     * 官方-迭代，队列（推荐用while+for）
     * 迭代，队列
     * 思路：图的广度优先搜索
     * @param root
     * @return
     */
    public static List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        if (root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<Integer>();
            int currentLevelSize = queue.size();
//            用for循环保证同层的节点在同一个List里
            for (int i = 1; i <= currentLevelSize; ++i) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            ret.add(level);
        }

        return ret;
    }

    static class TreeNode{

        private int val;

        private TreeNode left;

        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }
}
